package l.l.w.CodingInterviews;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

/**
 * Created by llw on 2018/3/30/0030.
 */
public class 把数组排成最小的数 {
    public String PrintMinNumber(int [] numbers) {
        ArrayList<Integer> list = new ArrayList<>(numbers.length);
        for(int i = 0; i < numbers.length; ++i){
            list.add(numbers[i]);
        }
        Collections.sort(list, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                String s1 = o1+""+o2;
                String s2 = o2+""+o1;
                return s1.compareTo(s2);
            }
        });
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < list.size(); ++i){
            sb.append(list.get(i));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        int[] nums = new int[]{3,32,321};
        String s = new 把数组排成最小的数().PrintMinNumber(nums);
        System.out.println(s);
    }
}
